<?php
namespace submodule\modules\content\components;

use Yii;
use yii\db\Schema;
use yii\db\Migration;

class Uninstaller extends Migration
{
	public $controller;
	
	public function __construct($controller){
		$this->controller = $controller;
		
	}
    //安装
    public function install($models, $field_tablename)
    {
		
		$moduleid = $modelids[] = $models[0]->modelid;
		if(isset($models[1])) $modelids[] = $models[1]->modelid;

		$tablename = $models[0]->tablename;

		//建表
        Yii::$app->db->createCommand()->createTable('{{%'.$tablename.'}}', [
            'id'=>  "pk",
			'catid'=>  "smallint(5) unsigned NOT NULL default '0'",
			'title'=>  "varchar(80) NOT NULL default ''",
			'listorder'=>  "tinyint(3) unsigned NOT NULL default '0'",
			'status'=>  "tinyint(2) unsigned NOT NULL default '1'",
			'addtime'=>  "int(10) unsigned NOT NULL default '0'",
			'updatetime'=>  "int(10) unsigned NOT NULL default '0'",
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();
		
        Yii::$app->db->createCommand()->createTable('{{%'.$tablename.'_data}}', [
            'id'=>  "mediumint(8) unsigned default '0'",
			'content'=>  "text default NULL",
        ],'ENGINE InnoDB DEFAULT CHARSET utf8')->execute();

		
		//记录字段
		$fields = ['modelid', 'name', 'field', 'formtype', 'fieldtype', 'fieldlength', 'listorder', 'disabled', 'primarytable', 'required', 'setting', 'param', 'api', 'allowinput'];

		
		$rows = [
		     [$moduleid, 'ID', 'id', 'primarykey', 'int', 10, 0, 0, 2, 1, '', '', '', 0], 
			 [$moduleid, '栏目', 'catid', 'category', 'int', 10, 0, 0, 2, 1, '', '', '', 1], 
			 [$moduleid, '标题', 'title', 'text', 'varchar', 80, 0, 0, 1, 1, '', '', '', 1], 
			 [$moduleid, '排序', 'listorder', 'text', 'int', 10, 0, 0, 1, 1, '{"defaultvalue":"0"}', '', '', 0], 
			 [$moduleid, '状态', 'status', 'switch', 'int', 10, 0, 0, 1, 1, '', '', '', 1], 
			 [$moduleid, '添加时间', 'addtime', 'text', 'int', 10, 0, 0, 1, 1, '', '', '', 0], 
			 [$moduleid, '更新时间', 'updatetime', 'text', 'int', 10, 0, 0, 1, 1, '', '', '', 0], 
			 [$moduleid, '内容', 'content', 'ueditor', '', 150, 0, 0, 0, 0, '', '', '', 1],
	    ];
		Yii::$app->db->createCommand()->batchInsert($field_tablename, $fields, $rows)->execute();
		return $modelids;
		
    }
    
	//新建
    public function create($models, $field_tablename){
		return self::install($models, $field_tablename);
		
	}
	
	//卸载
    public function uninstall()
    {
        $this->dropTable('{{store_product}}');
    }
}
